/**
 * 
 */
package com.istart.webmagic.guba;

import java.util.ArrayList;
import java.util.List;

import com.istart.webmagic.HtmlUtil;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * 获取文章表 的标题、作者、评论数、阅读数、发表日期、最后更新时间、发表地方
 */
public class GuBaNumPageProcessor implements PageProcessor {
	//http://guba.eastmoney.com/default_1.html
    public static final String URL_LIST = "http://guba\\.eastmoney\\.com/default_\\d+\\.html";
//    http://guba.eastmoney.com/news,gssz,257496576.html
//    http://guba.eastmoney.com/news,zsgjgdaxi,257494345.html
    public static final String URL_POST = "http://guba\\.eastmoney\\.com/news,\\w+,\\d+\\.html";

    public static final String URL_NEXT = "default_\\d+\\.html";
    private Site site = Site
            .me()
            .setDomain("guba.eastmoney.com/")
            .setSleepTime(3000)
            .setUserAgent(
                    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
    @Override
    public void process(Page page) {
            List<String> xx = new ArrayList<>();
       	 for (int i = 1; i < 3; i++) {
       		 xx.add("http://guba.eastmoney.com/default_" + i + ".html");
   		}
            page.addTargetRequests(xx);
        	//标题
            page.putField("标题", HtmlUtil.getTextFromHtml(page.getHtml().xpath("//ul[@class='newlist']//a[@class='note']").all()));
            //作者
            page.putField("作者", HtmlUtil.getTextFromHtml(page.getHtml().xpath("//ul[@class='newlist']//cite [@class='aut']/a").all()));
            //评论数
            page.putField("评论数", HtmlUtil.getTextFromHtml(page.getHtml().xpath("//ul[@class='newlist']//cite[1]").all()));
            //阅读数
            page.putField("阅读数", HtmlUtil.getTextFromHtml(page.getHtml().xpath("//ul[@class='newlist']//cite[2]").all()));
            //发表日期
            //发表时间
            page.putField("发表日期", HtmlUtil.getTextFromHtml(page.getHtml().xpath("//ul[@class='newlist']//cite [@class='date']").all()));
            //最后更新时间
            page.putField("最后更新时间", HtmlUtil.getTextFromHtml(page.getHtml().xpath("//ul[@class='newlist']//cite [@class='last']").all()));
            //发表地方
            page.putField("发表地方",HtmlUtil.getTextFromHtml( page.getHtml().xpath("//ul[@class='newlist']//span[@class='sub']/a[@class='balink']").all()));
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new GuBaNumPageProcessor())
        	.addUrl("http://guba.eastmoney.com/default_1.html")
        	.addPipeline(new JsonFilePipeline("G:/webmagic/"))
        	.thread(5)
            .run();
    }
}